package com.itheima.pinda.web.truck;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.pinda.DTO.truck.TruckDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.truck.PdTruck;
import com.itheima.pinda.mapstruct.TruckDTO2Entity;
import com.itheima.pinda.service.PdTruckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * TruckController
 *
 * @author llhuang10
 * @date 2021/10/2 15:35
 */
@Slf4j
@Validated
@Api(tags = "车辆管理")
@RestController
@RequestMapping("/base/truck")
public class TruckController {

    @Resource
    private PdTruckService pdTruckService;

    @Resource
    private TruckDTO2Entity truckDTO2Entity;

    /**
     * 添加车辆
     *
     * @param dto 车辆信息
     * @return 车辆信息
     */
    @PostMapping
    @ApiOperation("添加车辆")
    public TruckDto saveTruck(@Valid @RequestBody TruckDto dto) {
        return pdTruckService.saveTruck(dto);
    }

    /**
     * 根据id获取车辆详情
     *
     * @param id 车辆id
     * @return 车辆信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取车辆详情")
    public TruckDto findById(@NotBlank(message = "车辆Id不能为空") @PathVariable(name = "id") String id) {
        PdTruck pdTruck = pdTruckService.getById(id);
        return truckDTO2Entity.conver(pdTruck);
    }

    /**
     * 获取车辆分页数据
     *
     * @param page         页码
     * @param pageSize     页尺寸
     * @param truckTypeId  车辆类型id
     * @param licensePlate 车牌号码
     * @param fleetId      所属车队Id
     * @return 车辆分页数据
     */
    @GetMapping("/page")
    @ApiOperation("获取车辆分页数据")
    public PageResponse<TruckDto> findByPage(@RequestParam(name = "page") Integer page,
                                             @RequestParam(name = "pageSize") Integer pageSize,
                                             @RequestParam(name = "truckTypeId", required = false) String truckTypeId,
                                             @RequestParam(name = "licensePlate", required = false) String licensePlate,
                                             @RequestParam(name = "fleetId", required = false) String fleetId) {
        return pdTruckService.findByPage(page, pageSize, truckTypeId, licensePlate, fleetId);
    }

    /**
     * 统计车辆数量
     *
     * @param fleetId 车队id
     * @return 车辆数量
     */
    @GetMapping("/count")
    @ApiOperation("统计车辆数量")
    public Integer count(@RequestParam(name = "fleetId", required = false) String fleetId) {
        return pdTruckService.count(Wrappers.<PdTruck>lambdaQuery()
                .eq(StringUtils.isNotBlank(fleetId), PdTruck::getFleetId, fleetId)
                .eq(PdTruck::getStatus, Constant.DATA_DEFAULT_STATUS));
    }

    /**
     * 获取车辆列表
     *
     * @param ids 车辆id列表
     * @return 车辆列表
     */
    @GetMapping("")
    @ApiOperation("获取车辆列表")
    public List<TruckDto> findAll(@RequestParam(name = "ids", required = false) List<String> ids,
                                  @RequestParam(name = "fleetId", required = false) String fleetId) {
        return pdTruckService.findAll(ids, fleetId);
    }

    /**
     * 更新车辆信息
     *
     * @param id  车辆id
     * @param dto 车辆信息
     * @return 车辆信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新车辆信息")
    public TruckDto update(@NotBlank(message = "车辆Id不能为空") @PathVariable(name = "id") String id,
                           @Valid @RequestBody TruckDto dto) {
        dto.setId(id);
        PdTruck pdTruck = truckDTO2Entity.conver(dto);
        pdTruckService.updateById(pdTruck);
        return dto;
    }

    /**
     * 删除车辆
     *
     * @param id 车辆id
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    @ApiOperation("删除车辆")
    public Result disable(@NotBlank(message = "车辆Id不能为空") @PathVariable(name = "id") String id) {
        pdTruckService.disableById(id);
        return Result.ok();
    }
}
